$('#xzh-btn-left2').on('click', function() {
    location.reload();
});


let allteachersId = [];//全选后所有的id
function xuanran(data) {
    
    $('.xzh-teacher-tbody').text('');

    for(var i = 0; i < data.length; i++) {
        var tr = `
            <tr class="xzh-td1">
                <td><input type="checkbox" class='xzh-danxuan' data-id=${data[i].teacherId}></td>
                <td>${data[i].teacherName}</td>
                <td>${data[i].teacherStanding}</td>
                <td>${data[i].sectionName}</td>
                <td>${data[i].teacherSex}</td>
                <td>${data[i].educationName}</td>
                <td>${data[i].teacherTel}</td>
                <td>${data[i].teacherEmail}</td>
                <td>${data[i].teacherEntry}</td>
                <td>
                <p class='chakan' data-id=${data[i].teacherId}>查看</p>
                <P class='bianji' data-id=${data[i].teacherId}>编辑</P>
                </td>
                
            </tr>
        `;

        $('.xzh-teacher-tbody').append($(tr));
    }
}

//请求数据库数据
function  initTeacher() {
    $.ajax({
        url: '/teachers/all',
        method: 'POST'
    })
    .done(function (res) {
        console.log('请求成功：', res);
        for(var i= 0 ; i < res.data.length; i++) {
            allteachersId.push(res.data[i].teacherId);
        }
        console.log(allteachersId);
        xuanran(res.data);
    })
    .fail(function (err) {
        console.log('请求失败：',err);
    })
}

initTeacher();

// 新增教师
$('#xzh-btn-right2').on('click', function () {
    location.href = 'teacherAdd.html';
})

//查看某个教师详细信息
$('.xzh-teacher-tbody').on('click', '.chakan', function() {
    const teacherId = $(this).attr('data-id');
    
    localStorage.setItem('teacherId',teacherId);

    location.href = 'teacherIntroduce.html';
});

//编辑信息
$('.xzh-teacher-tbody').on('click','.bianji', function () {
    const teacherId = $(this).attr('data-id');

    localStorage.setItem('teacherId',teacherId);

    location.href = 'teacherXiugai.html';
})


//删除老师

//全选
$('#xzh-quanxuan').on('click', function() {
    console.log($('#xzh-quanxuan').prop('checked'));
    if($('#xzh-quanxuan').prop('checked')) {
        $(':checkbox').prop('checked',true);
        teachersId = allteachersId;
        console.log('全选',teachersId);

    }else {
        $(':checkbox').prop('checked',false);

        teachersId = [];
        console.log('取消全选',teachersId);
        console.log('所有的固定的',allteachersId);
    }
});

// //单选
$('.xzh-teacher-tbody').on('click','.xzh-danxuan', function () {
    //全部单选被选中的数量
    var AllLength = $('.xzh-danxuan:checked').length;
    
    //全部单选的数量
    var AllDanLength = $('.xzh-danxuan').length;
    
    //如果数量相等全选就选中，反之取消
    if(AllLength == AllDanLength) {
        $('#xzh-quanxuan').prop('checked',true);
    }else {
        $('#xzh-quanxuan').prop('checked',false);
    }
});

//获取当前点击的id
var teachersId = [];
$('.xzh-teacher-tbody').on('click','.xzh-danxuan' ,function() {
    
    var teacherId = Number($(this).attr('data-id'));
    
    if(teachersId.indexOf(teacherId) == -1) {

        teachersId.push(teacherId);
    }else{
        for(var i = 0; i < teachersId.length; i++) {
            
            if(teacherId == teachersId[i]) {
                teachersId.splice(i ,1);
                console.log('删除后的:', teachersId);
                
            }
        }
        
    }
    
    console.log(teachersId);

});

//删除请求
$('.xzh-btn-left1').on('click',  function() {

    console.log(teachersId);

    for(var i = 0; i < teachersId.length; i++) {
        $.ajax({
            url: '/teachers/del',
            method: 'POST',
            data: {
                teacherIds: teachersId[i]
            }
        })

        .done(function (res) {
            console.log(res);
            initTeacher();
        })
        .fail(function(err) {
            console.log(err);
        })
    }

    teachersId = [];
    
    
});


// if($('#xzh-quanxuan').prop('checked')) {
    //     var teachers = [];
    //     $('#xzh-quanxuan').prop('checked',false);
        
    //     xuanran(teachers);
    // }else if ($('.xzh-danxuan:checked')) { 
        
    //     for(var i = 0; i < teachersId.length; i++) {
    //         danDel(teachersId[i]);
    //     }
    // }

//新增
// function addTeacher(teacherName,teacherTel,teacherBumen,teacherGongling,teacherEa,teacherSex,teacherJiaoyu,demoTest) {

//     var teachs = {
//         id: Date.now(),
//         name: teacherName,
//         section: teacherBumen,
//         sex: teacherSex,
//         teach: teacherJiaoyu,
//         tel: teacherTel,
//         email: teacherEa,
//         entry: demoTest,
//         standing: teacherGongling
//         // photo: teacherPhoto
//     };

//     teachers.unshift(teachs);

//     localStorage.setItem('teachers',JSON.stringify(teachers));
// }



// 确认新增
// $('.xzh-fix-right').on('click', function () {
    // var teacherName = $('.teacher-name').val();
    // var teacherTel = $('.teacher-tel').val();
    // var teacherBumen = $('#teacher-bumen').val();
    // var teacherGongling = $('#teacher-gongling').val();
    // var teacherEa = $('#teacher-ea').val();
    // var teacherSex = $('#teacher-sex').val();
    // var teacherJiaoyu = $('#teacher-jiaoyu').val();
    // var demoTest = $('#demoTest').val();
//     // var teacherPhoto = $('.xzh-headPhoto').val();
    
// //     //判断是否有输入内容
//     if(!teacherName || !teacherTel || !teacherBumen || !teacherGongling || !teacherEa || !teacherSex || !teacherJiaoyu || !demoTest) {
//        return alert('内容不能为空');
//     }

//     addTeacher(teacherName,teacherTel,teacherBumen,teacherGongling,teacherEa,teacherSex,teacherJiaoyu,demoTest);

//     $('.teacher-name').val('');
//     $('.teacher-tel').val('');
//     $('#teacher-bumen').val('');
//     $('#teacher-gongling').val('');
//     $('#teacher-ea').val('');
//     $('#teacher-sex').val('');
//     $('#teacher-jiaoyu').val('');
//     $('#demoTest').val('');

//     location.href = 'teacherPage.html';

//     xuanran(parent.getTeacher());
// });





//单选删除方法
// function danDel(id) {
//     var newTeachers = teachers.filter(function(item) {
//         return item.id != id;
//     });

//     console.log(newTeachers);

//     localStorage.setItem('teachers',JSON.stringify(newTeachers));

//     xuanran(newTeachers);
// }